package com.example.springbootredis.service.impl;


import com.example.springbootredis.entiy.Classroom;
import com.example.springbootredis.repository.ClassroomRepository;
import com.example.springbootredis.service.ClassroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ClassroomServiceImpl implements ClassroomService {

    @Autowired
    private ClassroomRepository classroomRepository;

    @Override
    @CacheEvict(value = "classrooms", allEntries = true)
    public Classroom save(Classroom classroom) {
        return classroomRepository.save(classroom);
    }

    @Override
    @Cacheable(value = "classrooms")
    public List<Classroom> findAll() {
        return classroomRepository.findAll();
    }

    @Override
    public Optional<Classroom> findById(Long id) {
        return classroomRepository.findById(id);
    }

    @Override
    @CacheEvict(value = "classrooms", allEntries = true)
    public void deleteById(Long id) {
        classroomRepository.deleteById(id);
    }

    @Override
    @Cacheable(value = "classrooms", key = "#campus")
    public List<Classroom> findByCampus(String campus) {
        return classroomRepository.findByCampus(campus);
    }

    @Override
    @Cacheable(value = "classrooms", key = "#classroomName")
    public List<Classroom> findByClassroomNameContaining(String classroomName) {
        return classroomRepository.findByClassroomNameContaining(classroomName);
    }

    @Override
    @Cacheable(value = "classrooms", key = "#campus + '-' + #classroomName")
    public List<Classroom> findByCampusAndClassroomNameContaining(String campus, String classroomName) {
        return classroomRepository.findByCampusAndClassroomNameContaining(campus, classroomName);
    }

    @Override
    public boolean existsByClassroomNo(String classroomNo) {
        return classroomRepository.existsByClassroomNo(classroomNo);
    }

    @Override
    public boolean existsByClassroomName(String classroomName) {
        return classroomRepository.existsByClassroomName(classroomName);
    }

    @Override
    public boolean existsByClassroomNoAndIdNot(String classroomNo, Long id) {
        return classroomRepository.existsByClassroomNoAndIdNot(classroomNo, id);
    }

    @Override
    public boolean existsByClassroomNameAndIdNot(String classroomName, Long id) {
        return classroomRepository.existsByClassroomNameAndIdNot(classroomName, id);
    }
}